﻿using ImpromptuInterface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace MVCUI
{
    public enum PreDefinedColumnsTypes
    {
        Delete, Edit, Select
    }

     public class DataGridColumns
    {
        List<Column> columns = new List<Column>();

        public void AddColumn(Column column)
        {
            columns.Add(column);
        }

        public List<Column> Columns
        {
            get { return columns; }
            set { columns = value; }
        }
    }

 

    public class TextColumn: Column
    {
        private Func<dynamic, dynamic> itemFormatData;
        public TextColumn(string headerName, string dataField, Func<dynamic, dynamic> itemFormat = null, object attributes = null)
            : base(headerName, dataField)
        {
            itemFormatData = itemFormat;
            Attributes = attributes;
        }

        public override dynamic GetHtml(dynamic value)
        {
            dynamic result = value;
            if (itemFormatData != null)
            {
                result = itemFormatData(value);
            }

            return result;
        }
    }



    public class PasswordColumn : Column
    {
        private Func<dynamic, dynamic> itemFormatData;
        private Func<dynamic, dynamic> itemEditFormatData;
        
        public PasswordColumn(string headerName, string dataField, Func<dynamic, dynamic> itemFormat = null, Func<dynamic, dynamic> itemEditor = null, object attributes = null)
            : base(headerName, dataField)
        {
            itemFormatData = itemFormat;
            itemEditFormatData = itemEditor;
            Attributes = attributes;
        }

        public override dynamic GetHtml(dynamic value)
        {
            dynamic result = "*****";
            if (itemFormatData != null)
            {
                result = itemFormatData(value);
            }

            return result;
        }
        public override dynamic Editor(string name, dynamic value, string objname)
        {
            dynamic result = "";
            if (itemEditFormatData != null)
            {
                result = itemEditFormatData(value);
            }
            else
            {
                result = String.Format("<input type='password' name='{1}.{2}' id='{1}.{2}' {3} value=''/>", value, objname, name, this.Attributes);
            }
            return result;
        }
    }


    public class DateColumn : Column
    {
        private Func<dynamic, dynamic> itemFormatData;
        private Func<dynamic, dynamic> itemEditFormatData;
        public DateColumn(string headerName, string dataField, Func<dynamic, dynamic> itemFormat = null,  Func<dynamic, dynamic> editFormat = null, object attributes = null)
            : base(headerName, dataField)
        {
            itemFormatData = itemFormat;
            itemEditFormatData = editFormat;
            Attributes = attributes;
        }

        public override dynamic GetHtml(dynamic value)
        {
            dynamic result = value ;
            if (itemFormatData != null)
            {
                result = itemFormatData(result);
            }

            return result;
        }

        public override dynamic Editor(string name, dynamic value, string obj)
        {
            dynamic result = "";
            if (itemEditFormatData != null)
            {
                result = itemEditFormatData(value);
            }
            else
            {
                result = String.Format("<input type='text' name='{1}.{2}' id='{1}.{2}' datepickergr='dt' {3} value='{0}'/>", value, obj, name, this.Attributes);
            }
            return result;
        }
    }
    public class DropDownColumn : Column
    {
        private Func<dynamic, dynamic> itemFormatData;
        private Func<dynamic, dynamic> editItemFormatData;

        public DropDownColumn(string headerName, string dataField, dynamic dataSource, string dataSourceText, string dataSourceValue, Func<dynamic, dynamic> itemFormat = null, Func<dynamic, dynamic> editFormat = null, object attributes = null)
            : base(headerName, dataField)
        {
            itemFormatData = itemFormat;
            editItemFormatData = editFormat;
            this.DataSource = dataSource;
            this.ListText = dataSourceText;
            this.ListValue = dataSourceValue;
            Attributes = attributes;
        }

        public override dynamic GetHtml(dynamic value)
        {
            dynamic result =  Impromptu.InvokeGet(value, this.ListText);
            if (itemFormatData != null)
            {
                result = itemFormatData(value);
            }

            return result;
        }

        public override dynamic Editor(string name, dynamic value, string obj, dynamic ds)
        {
            dynamic result = "";
            StringBuilder editor = new StringBuilder();
            if (editItemFormatData != null)
            {
                value = editItemFormatData(value);
            }
            else
            {
                editor.AppendLine(String.Format("<select name='{0}.{1}.{2}' id='{0}_{1}_{2}'>", obj, name, this.ListValue));
                editor.AppendLine(String.Format("<option  value='-1'>Select item...</option>"));
                foreach (dynamic item in ds)
                {
                    object valueField= Impromptu.InvokeGet(item, this.ListValue);
                    object text= Impromptu.InvokeGet(item, this.ListText);
                    if (text != Impromptu.InvokeGet(value, this.ListText))
                    {
                        editor.AppendLine(String.Format("<option value='{0}'>{1}</option>", valueField, text, obj, name));
                    }
                    else
                    {
                        editor.AppendLine(String.Format("<option selected='selected' value='{0}'>{1}</option>", valueField, text, obj, name));
                       
                    }
                }
                editor.AppendLine("</select>");
            }

       
            return editor;
        }
    }
    public class DropDownColumn2 : Column
    {
        private Func<dynamic, dynamic> itemFormatData;
        private Func<dynamic, dynamic> editItemFormatData;
        private string dataFieldId = "";

        public DropDownColumn2(string headerName, string dataField, string dataFieldId, dynamic dataSource, string dataSourceText, string dataSourceValue, Func<dynamic, dynamic> itemFormat = null, Func<dynamic, dynamic> editFormat = null, object attributes = null)
            : base(headerName, dataField)
        {
            itemFormatData = itemFormat;
            editItemFormatData = editFormat;
            this.DataSource = dataSource;
            this.ListText = dataSourceText;
            this.ListValue = dataSourceValue;
            this.dataFieldId = dataFieldId;
            Attributes = attributes;
        }

        public override dynamic GetHtml(dynamic value)
        {
            dynamic result = value;
            if (itemFormatData != null)
            {
                result = itemFormatData(value);
            }

            return result;
        }

        public override dynamic Editor(string name, dynamic value, string obj,  dynamic ds)
        {
            dynamic result = "";
            StringBuilder editor = new StringBuilder();
            if (editItemFormatData != null)
            {
                value = editItemFormatData(value);
            }
            else
            {
                editor.AppendLine(String.Format("<select name='{0}.{1}' id='{0}_{1}' {2}>", obj, this.dataFieldId, this.Attributes));
                editor.AppendLine(String.Format("<option  value>Select item...</option>"));
                foreach (dynamic item in ds)
                {
                    object valueField = Impromptu.InvokeGet(item, this.ListValue);
                    object text = Impromptu.InvokeGet(item, this.ListText);
                    if (text.ToString() != value)
                    {
                        editor.AppendLine(String.Format("<option value='{0}'>{1}</option>", valueField, text, obj, name));
                    }
                    else
                    {
                        editor.AppendLine(String.Format("<option selected='selected' value='{0}'>{1}</option>", valueField, text, obj, name));

                    }
                }
                editor.AppendLine("</select>");
            }


            return editor;
        }
    }
    public class CustomColumn : Column
    {
        private Func<dynamic, dynamic> itemFormatData;
        public CustomColumn(string headerName, string dataField, Func<dynamic, dynamic> itemFormat = null, object attributes = null)
            : base(headerName, dataField)
        {
            itemFormatData = itemFormat;
            Attributes = attributes;
        }

        public override dynamic GetHtml(dynamic value)
        {
            dynamic result = value;
            if (itemFormatData != null)
            {
                result = itemFormatData(value);
            }

            return result;
        }
    }
    public class PredefinedColumn : Column
    {
        PreDefinedColumnsTypes column;
        private Func<dynamic, dynamic> itemFormatData;
        public PredefinedColumn(PreDefinedColumnsTypes column, string headerName = "", Func<dynamic, dynamic> itemFormat = null, object attributes = null)
            : base(headerName, "")
        {
            this.column = column;
            this.HeaderName = headerName;
            itemFormatData = itemFormat;
        }

        public PreDefinedColumnsTypes Column
        {
            get { return column; }
            set { column = value; }
        
        }
        public override dynamic GetHtml(dynamic value)
        {
            dynamic result = null;
            if (itemFormatData != null)
            {
                result = itemFormatData(value);
            }
            return result;
        }


    }
    public class BoolColumn : Column
    {
        private Func<dynamic, dynamic> itemFormatData;
        public BoolColumn(string headerName, string dataField, Func<dynamic, dynamic> itemFormat = null, object attributes=null)
            : base(headerName, dataField)
        {
            itemFormatData = itemFormat;
        }
        public override dynamic GetHtml(dynamic value)
        {
            dynamic result = value;
            if (itemFormatData != null)
            {
                result = itemFormatData(value);
            }

            return result;
           
        }


        public override dynamic Editor(string name, dynamic value, string obj)
        {
            dynamic result = "";
            if (itemFormatData != null)
            {
                value = itemFormatData(value);
            }

            string selected="";
            if(value) {
                selected = "checked='checked'";
            }
            result = String.Format("<input type='checkbox' name='{1}.{2}' id='{1}.{2}' value='True'  {3}/>", value, obj, name, selected);
           // result = String.Format("<input type='text' name='{1}.{2}' id='{1}.{2}' datepickergr='dt' value='{0}'/>", value, obj, name);
            return result;
        }
    }

   public abstract class Column
    {
        string headerName = "";
        string dataField = "";
        GridConfiguration configuration =null;
        dynamic dataSource = null;
        string listText = "";
        string listValue = "";
        object attributes = "";

        public Column() { }
        public Column(string headerName, string dataField, GridConfiguration configuration= null, dynamic dataSource=null)
        {
            this.headerName = headerName;
            this.dataField = dataField;
            this.configuration = configuration;
            this.dataSource = dataSource;

        }

        public object Attributes
        {
            get { return attributes; }
            set {
                if (value != null)
                {
                    attributes = value.ToString().Replace("{", "").Replace("}", "");
                }
            }
        }

        public string ListText
        {
            get { return listText; }
            set { listText = value; }
        }

        private bool editable = true;
        public bool Editable
        {
            get { return editable; }
            set { editable = value; }
        }

        public string ListValue
        {
            get { return listValue; }
            set { listValue = value; }
        }

        public string HeaderName
        {
            get { return headerName; }
            set { headerName = value; }
        }

        public string DataField
        {
            get { return dataField; }
            set { dataField = value; }
        }

        public GridConfiguration Configuration
        {
            get { return configuration; }
            set { configuration = value; }
        }

             
        
        public dynamic DataSource
        {
            get { return dataSource; }
            set { dataSource = value; }

        }

        public virtual dynamic GetHtml(dynamic value)
        {
            return value;
        }

        public virtual dynamic Editor(string name, dynamic value, string objname)
        {
            return String.Format("<input {3} type='text' id='{2}_{1}' name='{2}.{1}' value='{0}'/>", value, name, objname, this.Attributes );
        }

        public virtual dynamic Editor(string name, dynamic value, string objname, dynamic ds)
        {
            return String.Format("<input {3} type='text' id='{2}_{1}' name='{2}.{1}' value='{0}'/>", value, name, objname, this.Attributes);
        }

 
    }

}
